# -*- coding: utf8 -*-


import json
import config
import pymongo
import datetime

from scrapy import Spider
from scrapy.http import Request
from sqlhelper import SqlHelper


class OkcoinKline(Spider):
    name = 'okcoin_kline'

    def __init__(self, name=None, **kwargs):
        super(OkcoinKline, self).__init__(name, **kwargs)

        # 连接mongodb
        # client = pymongo.MongoClient('localhost', 27017)
        client = pymongo.MongoClient('47.254.26.182', 27017)
        self.db = client.bt_news

    def start_requests(self):
        currency_pair = ['btc_usd', 'ltc_usd', 'eth_usd', 'etc_usd', 'bch_usd']

        for currency in currency_pair:
            url = 'https://www.okcoin.com/api/v1/depth.do?symbol=%s' % currency

            yield Request(
                url=url,
                dont_filter=True,
                headers={
                    # 'Host': 'block.cc',
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
                },
                meta={
                    'market_pair': currency
                },
                callback=self.depth_info
            )

    def depth_info(self, response):
        body = json.loads(response.body)
        depth_okcoin = body

        market_pair = response.meta.get('market_pair')

        url = 'https://www.okcoin.com/api/v1/trades.do?symbol=%s' % market_pair

        yield Request(
            url=url,
            dont_filter=True,
            headers={
                # 'Host': 'block.cc',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
            },
            meta={
                'market_pair': market_pair,
                'depth_okcoin': depth_okcoin
            },
            callback=self.market_info
        )

    def market_info(self, response):
        body = json.loads(response.body)
        market_info = body
        market = []

        for info in market_info:
            date = info.get('date')
            date_ms = info.get('date_ms')
            amount = info.get('amount')
            price = info.get('price')
            type = info.get('type')
            tid = info.get('tid')
            date = {
                'date': str(date),
                'date_ms': str(date_ms),
                'amount': amount,
                'price': price,
                'type': type,
                'tid': tid,
            }
            market.append(date)

        # print(market)

        market_pair = response.meta.get('market_pair')
        depth_okcoin = response.meta.get('depth_okcoin')

        type_min = ['1min', '3min', '5min', '15min', '30min', '1day', '3day', '1week', '1hour', '2hour', '4hour', '6hour', '12hour']
        for min in type_min:

            url = 'https://www.okcoin.com/api/v1/kline.do?symbol=%s&type=%s' % (market_pair, min)
            yield Request(
                url=url,
                dont_filter=True,
                headers={
                    # 'Host': 'block.cc',
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0',
                },
                meta={
                    'market_pair': market_pair,
                    'depth_okcoin': depth_okcoin,
                    'market_info': market,
                    'type': min
                },
                callback=self.exchange_info
            )

    def exchange_info(self, response):
        kline = json.loads(response.body)
        type = response.meta.get('type')

        line = []

        for info in kline:
            l = []
            for i in info:

                a = (str(i))
                l.append(a)

            line.append(l)

        market_pair = response.meta.get('market_pair')
        depth_okcoin = response.meta.get('depth_okcoin')
        market_info = response.meta.get('market_info')

        # 入库
        data = {
            'exchange_name': 'okcoin',
            'market_pair': market_pair,
            'depths': depth_okcoin,
            'trades': market_info,
            'lines': line,
            'type': type
        }

        # 入库mongo
        self.db.bt_kline.insert(data)


# 数据库
'''
'''